library(dplyr)
library(tidyr)
library(readstata13)

## set working directory to replication folder
survey <- read.dta13("Harris_Data/Harris 1972 Miami Presidential Election Survey, study no. 2233/harris_s2233_spss.dta")

# pid
survey$pid <- c(1:nrow(survey))

# study 
survey$study <- "2233"

# study year (year)
survey$year <- 1972

# geographic data (urban)
survey$urban <- as.character(survey$S13)
table(survey$urban)

# geographic data (region)
survey$region <- as.character(survey$S11)
table(survey$region)

# respondent head of household (hh)
survey$hh <- as.character(survey$F2A)
table(survey$hh)

# increasing inequality (inequality)
survey$inequality <- as.character(survey$P12_A)
table(survey$inequality)

# inequality variable (inequality.variable)
survey$inequality.variable <- 1
  
# union (union.self)
survey$union.self <- as.character(survey$F6_1) # already harmonized
  
# union (union.other) question is "no union member in family?"
survey$union.other <- as.character(recode(survey$F6_3, 
                             `Yes` = 'No',
                             `No` = 'Yes'))

table(survey$union.self)
table(survey$union.other)

# employment (employed)
survey$employed <- as.character(survey$F2B)
table(survey$employed)

# empl self
survey$employed.self <- NA

# occupation
survey$occupation <- as.character(survey$F2C)
table(survey$occupation)

# occ self
survey$occupation.self <- NA

# household size (hhsize)
survey$hhsize <- NA
  
# education (educ)
survey$educ <- as.character(survey$F5)                         

# household income (income)
survey$income <- as.character(survey$F9)
  
# age
survey$age <- as.character(survey$P1E)
  
# race
survey$race <- as.character(survey$F10)
  
# politics (party)
survey$party <- as.character(survey$P1C)
  
# politics (ideology)
survey$ideology <- NA
  
# gender
survey$gender <- as.character(survey$F11)
  
# religion
survey$religion <- as.character(survey$F7)
  
#factuals
survey$factual1 <- NA
survey$factual2 <- NA
survey$factual3 <- NA

## alienation index
survey$dontcare <- as.character(survey$P12_B)
survey$dontcount <- as.character(survey$P12_C)
survey$leftout <- as.character(survey$P12_E)

## question_place
survey$question_place <- "after party"

# subset
survey_2233 <- survey[,c("pid", "study", "year", "urban", "region", "hh",
                         "inequality", "inequality.variable", "union.self", "union.other",
                         "employed", "employed.self", "occupation", "occupation.self", "hhsize", "educ", "income", 
                         "age", "race", "party", "ideology", "gender", "religion",
                         "factual1", "factual2", "factual3", "dontcare", "dontcount", "leftout",
                         "question_place")]

# save file
#saveRDS(survey_2233, file = "Harris_Data/survey_2233.rds")
